package pl.edu.pw.stud.ltrzaska.policy.xml;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import pl.edu.pw.stud.ltrzaska.policy.entity.Filesystem;
import pl.edu.pw.stud.ltrzaska.policy.entity.FilesystemObject;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class FilesystemConverter implements Converter {
	private static final Logger LOGGER = Logger.getLogger(FilesystemConverter.class);

	@Override
	public boolean canConvert(Class clazz) {
		return (clazz == Filesystem.class);
	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		Filesystem fs = new Filesystem();

		ConverterCommons.preLoopUnmarshal(reader, fs);		
		while (reader.hasMoreChildren()) {
			
			reader.moveDown();
			String attrName = reader.getNodeName();

			if ("children".equals(attrName)) {
				fs.setChildren((ArrayList<FilesystemObject>) context.convertAnother(fs, List.class));
			}
			ConverterCommons.inLoopUnmarshal(reader, context, fs);
						
			reader.moveUp();
		}
		return fs;
	}

	@Override
	public void marshal(Object obj, HierarchicalStreamWriter hierarchicalstreamwriter,
			MarshallingContext marshallingcontext) {
		// TODO Auto-generated method stub
		
	}
	
}
